package com.wy.xinzhi.common.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.dao.IDataBaseDao;
import com.wy.framework.exceptions.ServiceException;
import com.wy.framework.services.AbstractBaseService;
import com.wy.framework.services.login.LoginService;
import com.wy.xinzhi.common.pojos.TCustomer;
import com.wy.xinzhi.common.pojos.TFoodRecord;

public class CustomerService extends AbstractBaseService {
	private static Logger logger = LoggerFactory.getLogger(LoginService.class);
	static PropertiesUtil p = new PropertiesUtil();
	
	Date d = new Date(System.currentTimeMillis()); // 年月日

	Date now = new Date();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
	String dqsj = dateFormat.format(now);

	private JdbcTemplate jdbcTemplate;
	private HibernateDaoSupport hibernateDao;
	private IDataBaseDao dataBaseDao;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public HibernateDaoSupport getHibernateDao() {
		return hibernateDao;
	}
	public void setHibernateDao(HibernateDaoSupport hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	public IDataBaseDao getDataBaseDao() {
		return dataBaseDao;
	}
	public void setDataBaseDao(IDataBaseDao dataBaseDao) {
		this.dataBaseDao = dataBaseDao;
	}
	
	/**
	 * tanc
	 * 查询客户信息列表分页
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<Map<String,Object>> getCustomer(String name,String sex,String phone,String beginDate,String endDate,int page,int pageSize) {
		
		StringBuffer sql = new StringBuffer("select * from t_customer where 1=1 ");
		List<Map<String, Object>> resultList = new ArrayList();
		if(!"".equals(name)&&name!=null)
		{
			sql.append(" and name like '%"+name+"%'");
		}
		if(!"".equals(sex)&&sex!=null)
		{
			sql.append(" and sex like '%"+sex+"%'");
		}
		if(!"".equals(phone)&&phone!=null)
		{
			sql.append(" and phone like '%"+phone+"%'");
		}
		if(!"".equals(beginDate)&&beginDate!=null)
		{
			sql.append(" and birthday >= '"+beginDate+"'");
		}
		if(!"".equals(endDate)&&endDate!=null)
		{
			sql.append(" and birthday <= '"+endDate+"'");
		}
		sql.append(" limit "+(page-1)*pageSize+","+page*pageSize);
		resultList = dataBaseDao.getJdbcTemplate().queryForList(sql.toString());
		logger.debug("查询客户信息列表，详细："+sql.toString());
		return resultList; 
	}
	
	/**
	 * tanc
	 * 查询客户信息列表总数
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int getCustomerTotal(String name,String sex,String phone,String beginDate,String endDate,int page,int pageSize) {
		
		StringBuffer sql = new StringBuffer("select count(1) as total from t_customer where 1=1 ");
		Map<String, Object> resultMap = new HashMap();
		if(!"".equals(name)&&name!=null)
		{
			sql.append(" and name like '%"+name+"%'");
		}
		if(!"".equals(sex)&&sex!=null)
		{
			sql.append(" and sex like '%"+sex+"%'");
		}
		if(!"".equals(phone)&&phone!=null)
		{
			sql.append(" and phone like '%"+phone+"%'");
		}
		if(!"".equals(beginDate)&&beginDate!=null)
		{
			sql.append(" and birthday >= '"+beginDate+"'");
		}
		if(!"".equals(endDate)&&endDate!=null)
		{
			sql.append(" and birthday <= '"+endDate+"'");
		}
		resultMap = dataBaseDao.getJdbcTemplate().queryForMap(sql.toString());
		int total = Integer.parseInt(resultMap.get("total").toString());
		return total; 
	}
	
	/**
	 * tanc
	 * 绑定客户信息
	 * @throws ServiceException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void addCustomer(TCustomer cust) throws ServiceException {
			try {
				hibernateDao.getHibernateTemplate().save(cust);
			} catch (DataAccessException e) {
				logger.error("系统错误:{}", e);
				throw new ServiceException(e);
			}
	}
	
	/**
	 * tanc
	 * 保存或者更新客户信息
	 * @throws ServiceException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void saveOrUpdateCustomer(TCustomer cust) throws ServiceException {
			try {
				hibernateDao.getHibernateTemplate().saveOrUpdate(cust);
			} catch (DataAccessException e) {
				logger.error("系统错误:{}", e);
				throw new ServiceException(e);
			}
	}
	
	/**
	 * tanc
	 * 判断用户是否存在
	 */
	public TCustomer loginValid(String openid) {
		TCustomer cust = null;
		List list = hibernateDao.getHibernateTemplate().find(" FROM TCustomer T WHERE T.customerId = ?", openid);
		if(list.size()>0)
		{
			cust=(TCustomer) list.get(0);
			return cust;
		}
		return null;
	}
	
	public void deleteCustById(String customerId)
	{
		TCustomer cust = new TCustomer();
		cust.setCustomerId(customerId);
		hibernateDao.getHibernateTemplate().delete(cust);
	}
	
}
